package cn.tedu.sp06.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@Slf4j
public class RibbonController {
    @Autowired
    private RestTemplate rt;
    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFB")//指定降级方法方法名
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return rt.getForObject("http://item-service/{1}",//item-service等效于localhost：8001/localhost：8002
                JsonResult.class,//将远程调用获取到的返回的结果由json类型转换为JsonResult类型（8001return传回的是一个Json字符串）
                orderId);//orderId 填充上面的占位符
    }
    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")//指定降级方法方法名
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        //用rt调用远程服务，发送post请求
        //商品数据放在协议体中，向远程服务提交
        return rt.postForObject("http://item-service/decreaseNumber",
                items,//发送给远程服务的数据，底层会将该数据自动转换为json格式发送出去
                JsonResult.class);//将返回的对象类型json转换为JsonResult类型
    }
    @GetMapping("/user-service/{userId}")
    @HystrixCommand(fallbackMethod = "getUserFB")//指定降级方法方法名
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return rt.getForObject("http://user-service/{1}",
                JsonResult.class,
                userId);
    }
    @GetMapping("/user-service/{userId}/score")
    @HystrixCommand(fallbackMethod = "addScoreFB")//指定降级方法方法名
    public JsonResult<?> addScore(@PathVariable Integer userId,Integer score){
        return rt.getForObject("http://user-service/{1}/score",
                JsonResult.class,
                userId);
    }
    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFB")//指定降级方法方法名
    public JsonResult<Order> getOrder(@PathVariable Integer orderId){
        return rt.getForObject("http://order-service/{1}",
                JsonResult.class,
                orderId);
    }
    @GetMapping("/order-service/")
    @HystrixCommand(fallbackMethod = "addOrderFB")//指定降级方法方法名
    public JsonResult<?> addOrder(){
        return rt.getForObject("http://order-service",
                JsonResult.class);
    }



    public JsonResult<List<Item>> getItemsFB(@PathVariable String orderId){//降级方法
        return JsonResult.err("获取订单的商品列表失败");
    }

    public JsonResult<?> decreaseNumberFB(@RequestBody List<Item> items){
        return JsonResult.err("减少商品库存失败");
    }

    public JsonResult<User> getUserFB(@PathVariable Integer userId){
        return JsonResult.err("获取用户信息失败");
    }
    public JsonResult<?> addScoreFB(@PathVariable Integer userId,Integer score){
        return JsonResult.err("增加用户积分失败");
    }

    public JsonResult<Order> getOrderFB(@PathVariable Integer orderId){
        return JsonResult.err("获取订单失败");
    }
    public JsonResult<?> addOrderFB(){
        return JsonResult.err("保存订单失败");
    }

}
